package cn.tedu.tea.service;

import cn.tedu.tea.domain.User;
import cn.tedu.tea.domain.param.UserAddNewDTO;
import cn.tedu.tea.domain.param.UserUpdateDTO;
import cn.tedu.tea.domain.vo.UserListVO;
import cn.tedu.tea.domain.vo.UserStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.UserMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加用户】的业务，参数： {}", userAddNewDTO);
        int count = userMapper.countByName(userAddNewDTO.getUserName());
        if (count > 0){
            String message = "添加用户失败，用户名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        User user = new User();
        BeanUtils.copyProperties(userAddNewDTO, user);
        user.setModifiedUserId(0);
        int rows = userMapper.insert(user);
        if (rows != 1){
            String message = "添加用户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除用户】的业务，参数： {}", id);
        UserStandardVO userStandard = userMapper.getStandardById(id);
        if (userStandard == null){
            String message = "删除用户失败，要删除的用户不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = userMapper.deleteById(id);
        if (rows != 1){
            String message = "删除用户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除用户】的业务，参数： {}", ids);
        int rows = userMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除用户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户信息】的业务，参数：{}", userUpdateDTO);
        UserStandardVO userStandard = userMapper.getStandardById(userUpdateDTO.getUserId());
        if (userStandard == null){
            String message = "修改用户失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = userMapper.countByNameAndNotId(userUpdateDTO.getUserName(), userUpdateDTO.getUserId());
        if (count > 0){
            String message = "修改用户失败，用户名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        int rows = userMapper.update(user);
        if (rows != 1){
            String message = "修改用户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将用户设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将用户设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public UserStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询用户详情】的业务，参数： {}", id);
        UserStandardVO userStandard = userMapper.getStandardById(id);
        if (userStandard == null){
            String message = "查询用户详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return userStandard;
    }

    @Override
    public List<UserListVO> list() {
        log.debug("开始处理【查询用户列表】的业务，无参数");
        List<UserListVO> list = userMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        UserStandardVO currentUser = userMapper.getStandardById(id);
        if (currentUser == null) {
            String message = STATUS_TEXT[status] + "用户失败，用户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentUser.getStatus() == status) {
            String message = STATUS_TEXT[status] + "用户失败，此用户已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        User updateUser = new User();
        updateUser.setUserId(id);
        updateUser.setStatus(status);
        int rows = userMapper.update(updateUser);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "用户失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
